﻿using System;

namespace NBlog.Web.Application.Service.Internal.Caching
{
    public interface IDefaultCachePolicy : ICachePolicy
    {
    }

    public class DefaultCachePolicy : IDefaultCachePolicy
    {
        private readonly ICacheProvider _cacheProvider;

        public DefaultCachePolicy(ICacheProvider cacheProvider)
        {
            _cacheProvider = cacheProvider;
        }

        public T Get<T>(string cacheKey, Func<T> getter)
        {
            var item = _cacheProvider.Get(cacheKey);

            if (item == null)
            {
                item = getter();
                _cacheProvider.Add(cacheKey, item, DateTime.Now.AddDays(2));
            }

            return (T)item;
        }
    }
}